package com.hfdg.prototype.base.ctrl;

import com.hfdg.authority.service.AuthUserService;
import com.hfdg.prototype.base.model.AjaxResult;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName BaseCtrl
 * @Description TODO
 * @Author yuelh
 * @Date 2020-1-19 16:24
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Controller
public class BaseCtrl {
    @Autowired
    AuthUserService authUserService;

    @RequestMapping("/")
    public String login(){
        return "login";
    }
    @RequestMapping("/home")
    public String home(Model model){

        return "home";
    }
    @RequestMapping("/dashboard")
    public String dashboard(){
        return "dashboard";
    }
    @RequestMapping("/userLogin")
    @ResponseBody
    public AjaxResult userLogin(String loginName, String loginPwd, HttpServletRequest request, Model model){
        Map<String,Object> userMap = authUserService.getAuthUserByLoginName(loginName);
        if(null!=userMap){

            if(DigestUtils.sha256Hex(loginPwd).equalsIgnoreCase(userMap.get("loginPwd").toString())){
                String token = UUID.randomUUID().toString().replaceAll("-","");
                for(String key:userMap.keySet()){
                    request.getSession().setAttribute(key,userMap.get(key));
                }
                userMap.put("token",token);
               return AjaxResult.success(userMap);
            }else{
            return AjaxResult.error(99,"密码凭证错误，请输入正确的密码！");
            }
        }else{
            return AjaxResult.error(99,"账号不存在！");
        }
    }
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request, SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "login";
    }
}
